package com.example.wechat_b.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import com.example.wechat_b.R;
import com.example.wechat_b.bean.FriendQBean;

import java.util.List;

/**
 * @author abbas
 * @brief 朋友圈的ListViewAdapter
 * @date 2021-11-09
 */
public class FriendQListAdapter extends BaseAdapter {

    private List<FriendQBean> list;
    private Context context;

    public FriendQListAdapter(Context context,List<FriendQBean> list){
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
    *
    *@descrption 无脑写完写了N遍的ListViewHolder 优化（公式）模式 结果发现ListView包含的GridView当中还需解析另一个View 这...？
     *           11点半还要多了，不搞了。
     *           额，凑合一下搞了
    *@time abbas abbas
    */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.friend_listview_item,null);
            viewHolder.photo = convertView.findViewById(R.id.friend_listview_item_photo);
            viewHolder.img = convertView.findViewById(R.id.friend_listview_item_img);
            viewHolder.name = convertView.findViewById(R.id.friend_listview_item_name);
            viewHolder.description = convertView.findViewById(R.id.friend_listview_item_text);
            viewHolder.local = convertView.findViewById(R.id.friend_listview_item_local);
            viewHolder.time = convertView.findViewById(R.id.friend_listview_item_time);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        FriendQBean bean = list.get(position);
        viewHolder.photo.setImageResource(bean.getPhoto());
        if (bean.getImg() != 0){
            viewHolder.img.setImageResource(bean.getImg());
            viewHolder.img.setVisibility(View.VISIBLE);
        }
        viewHolder.name.setText(bean.getName());
        viewHolder.description.setText(bean.getDescription());
        viewHolder.local.setText(bean.getLocal());
        viewHolder.time.setText(bean.getTime());

        return convertView;
    }

    class ViewHolder{
        //p1,p2,p3,p4,p5,p6,p7,p8,p9
        public ImageView photo,img;
        public TextView name,description,local,time;
    }
}
